package com.videoaggre.zcl.ui.activity

import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import com.videoaggre.zcl.api.ApiCode
import com.videoaggre.zcl.api.ApiProcess
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import java.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

class TestActivity : BaseActivity() {
    fun xxxx() {
        lifecycleScope.launch(CoroutineExceptionHandler { _, throwable ->
            // 异常接收
        }) {
            val condition1Value = condition1()
            if (condition1Value) {
                condition2()
            }
        }
    }

    /**
     * 条件1
     */
    private suspend fun condition1() = suspendCancellableCoroutine<Boolean> { coroutine ->
        AlertDialog.Builder(this)
            .setMessage("测试消息1")
            .setPositiveButton("确定") { dialog: DialogInterface, which: Int ->
                coroutine.resume(true)
            }
            .setNegativeButton("取消") { dialog: DialogInterface, which: Int ->
                coroutine.resumeWithException(RuntimeException("中断后续操作"))
            }
            .create()
            .show()
    }

    /**
     * 条件2
     */
    private suspend fun condition2() = suspendCancellableCoroutine<Boolean> { coroutine ->
        AlertDialog.Builder(this)
            .setMessage("测试消息2")
            .setPositiveButton("确定") { dialog: DialogInterface, which: Int ->
                coroutine.resume(true)
            }
            .setNegativeButton("取消") { dialog: DialogInterface, which: Int ->
                coroutine.resumeWithException(RuntimeException("中断后续操作"))
            }
            .create()
            .show()
    }
}